package com.leedsoft.news2.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.leedsoft.news2.R;
import com.leedsoft.news2.ViewPagerIndicator;
import com.leedsoft.news2.model.Channel;
import com.leedsoft.news2.model.ShowapiResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/4.
 */
public class NewsFragment extends Fragment {
    private RequestQueue mQueue;
    private List<Fragment> mTabCobtents = new ArrayList<Fragment>();
    private FragmentPagerAdapter mAdapter;
    private ViewPager mViewPager;
    private ViewPagerIndicator mIndicator;

    private List<Channel> mDatas;
    private List<String> mTitles;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.news_layout, container, false);
        initView(view);
        loadNewsChannels();
        return view;
    }

    private void loadNewsChannels() {
        mQueue = Volley.newRequestQueue(getActivity());
        String url = "https://route.showapi.com/109-34?" +
                "showapi_appid=28581&showapi_sign=afd21e489f474e34b92024bc0faf247c";
        StringRequest req = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //当请求发送成功后的回调  主线程中运行
                //解析JSON  获取List<Music>
                System.out.println(">>>>>>>>" + response);
                Gson gson = new Gson();
                ShowapiResponse res = gson.fromJson(String.valueOf(response), ShowapiResponse.class);
                mDatas = res.getShowapi_res_body().getChannelList();
                mTitles = new ArrayList<String>();
                for (Channel channel : mDatas) {
                    String name = channel.getName();
                    if (name.length() >= 4) name = name.substring(0, name.length() - 2);
                    mTitles.add(name);
                }
                initDatas();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // 当请求发送失败的回调
            }
        });
        mQueue.add(req);
    }

    private void initDatas() {
        for (Channel data : mDatas) {
            NewsListFragment fragment = NewsListFragment.newInstance(data.getChannelId());
            mTabCobtents.add(fragment);

        }
        mAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return mTabCobtents.size();
            }

            @Override
            public Fragment getItem(int position) {
                return mTabCobtents.get(position);
            }
        };
        //设置Tab上的标题
        mIndicator.setTabItemTitles(mTitles);
        mViewPager.setAdapter(mAdapter);

        //设置关联的ViewPager
        mIndicator.setViewPager(mViewPager, 0);
    }

    private void initView(View view) {
        mViewPager = (ViewPager) view.findViewById(R.id.id_vp2);
        mIndicator = (ViewPagerIndicator) view.findViewById(R.id.btn_indicator);
    }

}
